package com.example.mobileprojecttest;

import java.util.ArrayList;

import com.example.mobileprojecttest.taxista.activities.AlterarTaxistaActivity;
import com.example.mobileprojecttest.taxista.activities.CadastrarTaxistaActivity;
import com.example.mobileprojecttest.taxista.activities.GerenciarAdapter;
import com.example.mobileprojecttest.taxista.activities.TaxistaAdapter;
import com.example.mobileprojecttest.usuario.activities.AlterarUsuarioActivity;
import com.example.mobileprojecttest.usuario.activities.CadastrarUsuarioActivity;
import com.example.mobileprojetctteste.pessoa.ControladorPessoa;
import com.example.mobileprojetctteste.pessoa.Pessoa;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class GerenciarClientes extends Activity {
	private ListView listViewClientes;
	private GerenciarAdapter adapter;
	private ArrayList<Pessoa> listaClientes;
	private ControladorPessoa controle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gerenciar_clientes);
		
		setTitle("Gerenciar clientes");
		
		controle = new ControladorPessoa(getApplicationContext());

		listViewClientes = (ListView) findViewById(R.id.list_gerenciar_clientes);

		Pessoa pessoa = new Pessoa();
		pessoa.setTipo_usuario("C");
		listaClientes = controle.getTaxistas(pessoa);

		if (listaClientes != null) {
			adapter = new GerenciarAdapter(this, listaClientes);

			listViewClientes.setAdapter(adapter);
		}

		// seta para apresentar menu de contexto na lista
		registerForContextMenu(listViewClientes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.gerenciar_clientes, menu);
		return true;
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.item_gerenciar_taxistas:
	           Intent gc = new Intent(getApplicationContext(), PrincipalAdm.class);
	           startActivity(gc);
	        default:
	            return super.onContextItemSelected(item);
	    }
	}
	
	@Override
	protected void onResume() {
		Pessoa pessoa  = new Pessoa();
		pessoa.setTipo_usuario("C");
		listaClientes = controle.getTaxistas(pessoa);

		
		if(listaClientes != null){
			adapter = new GerenciarAdapter(this, listaClientes);
			
			listViewClientes.setAdapter(adapter);	
		}
		super.onResume();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.context_menu_taxista, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    switch (item.getItemId()) {
	        case R.id.item_edit:
	            edit(info.position);
	            return true;
	        case R.id.item_delete:
	            delete(info.position);
	            return true;
	        case R.id.item_add:
	            novo();
	            return true;
	        default:
	            return super.onContextItemSelected(item);
	    }
	}
	
	private void edit(long id){
		Pessoa pessoa = (Pessoa)adapter.getItem((int)id);
		
		Intent intent = new Intent(getApplicationContext(), AlterarUsuarioActivity.class);
		
		intent.putExtra("pessoa", pessoa);
		
		startActivity(intent);
	}
	
	private void delete(long id){
		Pessoa pessoa = (Pessoa)adapter.getItem((int)id);
		
		controle.delete(pessoa);
		
		onResume();
	}
	
	private void novo(){
		Intent intent = new Intent(getApplicationContext(), CadastrarUsuarioActivity.class);
		startActivity(intent);
	}
}
